## Make PSRM Table 1, Comparison of two respondents in Los Angeles
## Andrew Reeves and Ryan T. Moore
## First: 27 January 2017
## Last: 19 January 2020
library(Hmisc)
library(plyr)
library(maps)
library(xtable)

## Load required data:
load("table1-LAContext.Rdata")


## Table 1
## Build Table
table1 <- data.frame("p1.milieu" = rep(NA, 4), "p1.tract" =rep(NA, 4), "p2.milieu" =rep(NA, 4), "p2.tract"=rep(NA, 4), "la.county"=rep(NA, 4), "calif"=rep(NA, 4))
rownames(table1) <- c("White (not Hispanic)", "Black", "Hispanic / Latino", "Asian")

## Milieu (pct white)
pwhite <- ddply(opLocsLAwContext, ~uid, function(df) wtd.mean(df$pctwhite, df$pop100))
# uid        V1
# 1 396b4b80629c4a0dca44dfd7063df15f 0.6456515
# 2 958cd8223abd4573d906e126d840dac6 0.2561204
table1[1,1] <- round(pwhite[2,2], 2)
table1[1,3] <- round(pwhite[1,2], 2)

## Milieu (pct black)
pblack <- ddply(opLocsLAwContext, ~uid, function(df) wtd.mean(df$pctblack, df$pop100))
# uid         V1
# 1 396b4b80629c4a0dca44dfd7063df15f 0.05876393
# 2 958cd8223abd4573d906e126d840dac6 0.14264223
table1[2,1] <- round(pblack[2,2], 2)
table1[2,3] <- round(pblack[1,2], 2)


## Milieu (pct hispanic)
phisp <- ddply(opLocsLAwContext, ~uid, function(df) wtd.mean(df$pcthisp, df$pop100))
# uid         V1
# 1 396b4b80629c4a0dca44dfd7063df15f 0.05049048
# 2 958cd8223abd4573d906e126d840dac6 0.19154857
table1[3,1] <- round(phisp[2,2], 2)
table1[3,3] <- round(phisp[1,2], 2)


## Milieu (pct asian)
pasian <- ddply(opLocsLAwContext, ~uid, function(df) wtd.mean(df$pctasian, df$pop100))
# uid        V1
# 1 396b4b80629c4a0dca44dfd7063df15f 0.1858586
# 2 958cd8223abd4573d906e126d840dac6 0.1189361
table1[4,1] <- round(pasian[2,2], 2)
table1[4,3] <- round(pasian[1,2], 2)

## Information on Census Tract 3005.02, Los Angeles County, California: 
## Information on Census Tract 2696.02, Los Angeles County, California: 
### https://factfinder.census.gov/bkmk/table/1.0/en/ACS/12_5YR/B03002/1400000US06037269602|1400000US06037300502
## White
table1[1,2] <- round(1018/3207, 2)
table1[1,4] <- round(3081/5984, 2)
## Black
table1[2,2] <- round(412/3207, 2)
table1[2,4] <- round(43/5984, 2)
## Hispanic
table1[3,2] <- round(1025/3207, 2)
table1[3,4] <- round(718/5984, 2)
## Asian
table1[4,2] <- round(56/3207, 2)
table1[4,4] <- round(1682/5984, 2)

## County
table1[1,5] <- round(unique(opLocsLAwContext$pctwhite.cty), 2)
table1[2,5] <- round(unique(opLocsLAwContext$pctblack.cty), 2)
table1[3,5] <- round(unique(opLocsLAwContext$pcthisp.cty), 2)
table1[4,5] <- round(unique(opLocsLAwContext$pctasian.cty), 2)
## State
table1[1,6] <- round(unique(opLocsLAwContext$pctwhite.st), 2)
table1[2,6] <- round(unique(opLocsLAwContext$pctblack.st), 2)
table1[3,6] <- round(unique(opLocsLAwContext$pcthisp.st), 2)
table1[4,6] <- round(unique(opLocsLAwContext$pctasian.st), 2)

print(xtable(table1, type = "latex"), file ="Table-1.tex")

## Information on California: https://factfinder.census.gov/faces/tableservices/jsf/pages/productview.xhtml?src=CF
## Information on Los Angeles County: http://www.census.gov/quickfacts/table/PST045216/06037

